import axios, { AxiosRequestConfig } from 'axios';
import store from '../store';

axios.interceptors.request.use(config => {
  store.commit('setLoading', true);
  store.commit('setError', { status: false, message: '' });
  return config;
});

axios.interceptors.response.use(
  config => {
    setTimeout(() => {
      store.commit('setLoading', false);
    }, 200);
    return config;
  },
  err => {
    store.commit('setError', { status: true, ...err.response.data });
    store.commit('setLoading', false);
    return Promise.reject(err);
  }
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function fetchData(url: string, config: AxiosRequestConfig = { method: 'GET' }): Promise<any> {
  return await axios(url, config);
}

export default fetchData;
